package com.ruoyi.project.baoxiu.card.controller;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.baoxiu.card.domain.OrderWarrantyCard;
import com.ruoyi.project.baoxiu.card.service.IOrderWarrantyCardService;
import com.ruoyi.project.baoxiu.moban.domain.WarrantyCard;
import com.ruoyi.project.baoxiu.moban.service.IWarrantyCardService;
import com.ruoyi.project.dentalOrder.domain.DentalOrder;
import com.ruoyi.project.dentalOrder.service.IDentalOrderService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 保修订单Controller
 * 
 * @author wyh
 * @date 2024-08-17
 */
@RestController
@RequestMapping("/baoxiu/card/api")
public class OrderWarrantyCardApiController extends BaseController
{
    @Autowired
    private IOrderWarrantyCardService orderWarrantyCardService;

    @Autowired
    private IDentalOrderService dentalOrderService;

    @Autowired
    private IWarrantyCardService warrantyCardService;

    /**
     * 查询保修订单列表
     */
    @PreAuthorize("@ss.hasPermi('baoxiu:card:list')")
    @GetMapping("/list")
    public TableDataInfo list(OrderWarrantyCard orderWarrantyCard)
    {
        startPage();
        List<OrderWarrantyCard> list = orderWarrantyCardService.selectOrderWarrantyCardList(orderWarrantyCard);
        return getDataTable(list);
    }

    /**
     * 获取保修订单详细信息
     */
    @GetMapping(value = "/baoxiuCard")
    public AjaxResult getInfo(String outTradeNo)
    {
        if(StringUtils.isBlank(outTradeNo)){
            return error("订单号不能为空");
        }
        OrderWarrantyCard orderWarrantyCard = new OrderWarrantyCard();
        orderWarrantyCard.setOutTradeNo(outTradeNo);
        List<OrderWarrantyCard> orderWarrantyCards = orderWarrantyCardService.selectOrderWarrantyCardList(orderWarrantyCard);
        return success(orderWarrantyCards.get(0));
    }


    /**
     * 新增保修订单
     */
    @PreAuthorize("@ss.hasPermi('baoxiu:card:add')")
    @Log(title = "保修订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderWarrantyCard orderWarrantyCard)
    {
        //校验订单
        String outTradeNo = orderWarrantyCard.getOutTradeNo();
        DentalOrder dentalOrder = dentalOrderService.selectDentalOrderByOutTradeNo(outTradeNo);
        if(ObjectUtils.isEmpty(dentalOrder)){
            return error("订单不存在");
        }

        //校验保修卡是否存在
        OrderWarrantyCard orderWarrantyCard1 = new OrderWarrantyCard();
        orderWarrantyCard1.setOutTradeNo(outTradeNo);
        List<OrderWarrantyCard> orderWarrantyCards = orderWarrantyCardService.selectOrderWarrantyCardList(orderWarrantyCard1);
        if(ObjectUtils.isNotEmpty(orderWarrantyCards)){
            return error("保修卡已存在");
        }

        //校验保修卡参数
        Long bxDate = orderWarrantyCard.getBxDate();
        if(bxDate == null){
            return error("保修天数不能为空");
        }
        String menzhen = orderWarrantyCard.getMenzhen();
        if(StringUtils.isBlank(menzhen)){
            return error("门诊参数不能为空");
        }
        String doctor = orderWarrantyCard.getDoctor();
        if(StringUtils.isBlank(doctor)){
            return error("医生参数不能为空");
        }
        String huanzhe = orderWarrantyCard.getHuanzhe();
        if(StringUtils.isBlank(huanzhe)){
            return error("患者参数不能为空");
        }
        String position = orderWarrantyCard.getPosition();
        if(StringUtils.isBlank(position)){
            return error("承制部位不能为空");
        }

        WarrantyCard warrantyCard = warrantyCardService.selectWarrantyCardById(1L);
        String content = warrantyCard.getContent();
        content = content.replace("{bxDate}",bxDate.toString());
        content = content.replace("{menzhen}",menzhen);
        content = content.replace("{doctor}",doctor);
        content = content.replace("{huanzhe}",huanzhe);
        content = content.replace("{position}",position);
        Date nowDate = DateUtils.getNowDate();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String formattedDate = dateFormat.format(nowDate);
        content = content.replace("{createTime}",formattedDate);
        orderWarrantyCard.setContent(content);

        Long userId = SecurityUtils.getUserId();
        orderWarrantyCard.setUserId(userId);

        return toAjax(orderWarrantyCardService.insertOrderWarrantyCard(orderWarrantyCard));
    }

}
